package com.example.a80205050.myapplication.MyActivity;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.icu.util.Calendar;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.example.a80205050.myapplication.Dao.InaccountDao;
import com.example.a80205050.myapplication.Dao.OutaccountDao;
import com.example.a80205050.myapplication.Modle.Tb_inaccount;
import com.example.a80205050.myapplication.Modle.Tb_outaccount;

/**
 * Created by 80205050 on 2017/11/1.
 */

public class InfoManage extends Activity {
    protected static final int DATA_DIALOG_ID=0;
    TextView tvtitle,textView;
    EditText txtMoney,txtTime,txtHA,txtMark;
    Spinner spType;
    Button btnEdit,btnDel;
    String[] strInfos;
    String strId,strType;
    private int mYear;
    private int mMonth;
    private int mDay;
    OutaccountDao outaccountDao=new OutaccountDao(InfoManage.this);
    InaccountDao  inaccountDao=new InaccountDao(InfoManage.this);

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.infomanage);//设置布局文件
        tvtitle=(TextView)findViewById(R.id.inouttitle);//获取标题标签对象
        textView=(TextView)findViewById(R.id.tvInOut);//获取地点/付款方标签对象
        txtMoney=(EditText)findViewById(R.id.txtInOutMoney);//获取金额的文本框
        txtTime=(EditText)findViewById(R.id.txtInOutTime);//获取的时间的文本框
        txtHA=(EditText)findViewById(R.id.txtInOut);//获取地点/付款方的文本框
        txtMark=(EditText)findViewById(R.id.txtInOutMark);//获取备注的文本框
        btnEdit=(Button)findViewById(R.id.btnInOutEdit);//获取修改的按钮
        btnDel=(Button)findViewById(R.id.btnInOutDelete);//获取删除的按钮
        spType=(Spinner)findViewById(R.id.spInOutType);
        Intent intent=getIntent();


        Bundle bundle=intent.getExtras();
        strInfos=bundle.getStringArray(Showinfo.FLAG);

        strId=strInfos[0];
        strType=strInfos[1];
        if(strType.equals("btnoutinfo")){
            tvtitle.setText("支出管理");
            textView.setText("地 点");
            Tb_outaccount tb_outaccount=outaccountDao.find(Integer.parseInt(strId));
            txtMoney.setText(String.valueOf(tb_outaccount.getMoney()));
            txtTime.setText(tb_outaccount.getTime());
            spType.setPrompt(tb_outaccount.getType());
            txtHA.setText(tb_outaccount.getAddress());
            txtMark.setText(tb_outaccount.getMark());
        }else if(strType.equals("btnininfo")){
            tvtitle.setText("收入管理");
            textView.setText("付款方");
            Tb_inaccount tb_inaccount=inaccountDao.find(Integer.parseInt(strId));
            txtMoney.setText(String.valueOf(tb_inaccount.getMoney()));
            txtTime.setText(tb_inaccount.getTime());
            spType.setPrompt(tb_inaccount.getType());
            txtHA.setText(tb_inaccount.getHandler());
            txtMark.setText(tb_inaccount.getMark());
        }
        btnEdit.setOnClickListener(new View.OnClickListener() {// 为修改按钮设置监听事件
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (strType.equals("btnoutinfo"))// 判断类型如果是btnoutinfo
                {
                    Tb_outaccount tb_outaccount = new Tb_outaccount();// 创建Tb_outaccount对象
                    tb_outaccount.set_id(Integer.parseInt(strId));// 设置编号
                    tb_outaccount.setMoney(Double.parseDouble(txtMoney
                            .getText().toString()));// 设置金额
                    tb_outaccount.setTime(txtTime.getText().toString());// 设置时间
                    tb_outaccount.setType(spType.getSelectedItem().toString());// 设置类别
                    tb_outaccount.setAddress(txtHA.getText().toString());// 设置地点
                    tb_outaccount.setMark(txtMark.getText().toString());// 设置备注
                    outaccountDao.update(tb_outaccount);// 更新支出信息
                } else if (strType.equals("btnininfo"))// 判断类型如果是btnininfo
                {
                    Tb_inaccount tb_inaccount = new Tb_inaccount();// 创建Tb_inaccount对象
                    tb_inaccount.set_id(Integer.parseInt(strId));// 设置编号
                    tb_inaccount.setMoney(Double.parseDouble(txtMoney.getText()
                            .toString()));// 设置金额
                    tb_inaccount.setTime(txtTime.getText().toString());// 设置时间
                    tb_inaccount.setType(spType.getSelectedItem().toString());// 设置类别
                    tb_inaccount.setHandler(txtHA.getText().toString());// 设置付款方
                    tb_inaccount.setMark(txtMark.getText().toString());// 设置备注
                    inaccountDao.update(tb_inaccount);// 更新收入信息
                }
                // 弹出信息提示
                Toast.makeText(InfoManage.this, "数据修改成功！", Toast.LENGTH_SHORT)
                        .show();
            }
        });

        btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(strType.equals("btnoutinfo")){
                    outaccountDao.delete(Integer.parseInt(strId));
                }else if(strType.equals("btnininfo")){
                    inaccountDao.delete(Integer.parseInt(strId));
                }
                Toast.makeText(InfoManage.this,"数据删除成功",Toast.LENGTH_LONG).show();
            }
        });
        final Calendar c=Calendar.getInstance();
        mYear=c.get(Calendar.YEAR);
        mMonth=c.get(Calendar.MONTH);
        mDay=c.get(Calendar.DATE);
        updateDisplay();
    }

    @Override
    protected Dialog onCreateDialog(int id) {
       switch (id){
           case DATA_DIALOG_ID:
               return new DatePickerDialog(this,mDateSetListener,mYear,mMonth,mDay);
       }
        return null;
    }
    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            mYear = year;// 为年份赋值
            mMonth = monthOfYear;// 为月份赋值
            mDay = dayOfMonth;// 为天赋值
            updateDisplay();// 显示设置的日期
        }
    };
    private void updateDisplay() {
        // 显示设置的时间
        txtTime.setText(new StringBuilder().append(mYear).append("-")
                .append(mMonth + 1).append("-").append(mDay));
    }
}
